package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.model.wrappedrequests.SendMessageRequest;

public class EmployeeServiceClient extends ServiceClientBase {

	public EmployeeServiceClient(Configuration configuration) {
		super(configuration);
		// TODO Auto-generated constructor stub
	}

	public Employee GetEmployee(int employeeId) throws ServiceUnavailableException{
		Employee employee = null;
		String url = getMethodURL() + "/" + String.valueOf(employeeId);
		employee = (Employee) get(url, Employee.class);
		return employee;
	}
	
	public List<Employee> GetEmployeeList() throws ServiceUnavailableException{
		List<Employee> employeeList = new ArrayList<Employee>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Employee>>() {
		}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				employeeList.add((Employee) obj);
			}
		} else
			return null;
		return employeeList;
	}
	
	public Message GetMessage(int messageId) throws ServiceUnavailableException{
		Message message = null;
		String url = getMethodURL() + "/" + String.valueOf(messageId);
		message = (Message) get(url, Message.class);
		return message;
	}
	
	public void DeleteMessage(Integer messageId) throws ServiceUnavailableException{
		String url = getMethodURL();
		post(url, messageId);
	}
	
	public List<Message> GetMessageList(int employeeId) throws ServiceUnavailableException{
		List<Message> messageList = new ArrayList<Message>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Message>>() {
		}.getType();
		String url = getMethodURL() + "/" + String.valueOf(employeeId);
		objectList = getList(url, listType);
		if (objectList != null) {
			for (Object obj : objectList) {
				messageList.add((Message) obj);
			}
		} else
			return null;
		return messageList;
	}
	
	public void sendMessage(int senderId, int recipientId, String subject, String content) throws ServiceUnavailableException{
		SendMessageRequest req = new SendMessageRequest(senderId,  recipientId,  subject,  content);
		String url = getMethodURL();
		post(url, req);
	}
	
	public void readMessage(Integer messageId) throws ServiceUnavailableException{
		String url = getMethodURL();
		post(url, messageId);
	}
}
